package com.miraclengine.karma;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackTextureRegionLibrary;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.modifier.IModifier;

import com.miraclengine.eventobject.EventEntity;
import com.miraclengine.texture.TextureData;

public class TitleScene extends AbstractScene {
	
	private String TAG = TitleScene.class.getSimpleName();

	private KarmaGame game;
	private TexturePackTextureRegionLibrary packer;
	
	//TextureRegion
	private TextureRegion bgRegion;
	private TextureRegion titleRegion;
	private TextureRegion copyrightRegion;
	private TextureRegion btnStartNormalRegion;
	private TextureRegion btnResetNormalRegion;
	private TextureRegion btnStartSelectRegion;
	private TextureRegion btnResetSelectRegion;
	
	private TextureRegion achieveBackRegion;
	private TextureRegion bookmarkBackRegion;
	private TextureRegion btnSocialNormalRegion;
	private TextureRegion treeStartNormalRegion;
	private TextureRegion bookmarkBoxRegion;
	
	//Sprite
	private Sprite bg;
	private Sprite title;
	private Sprite copyright;
	private ButtonSprite btnStart;
	private ButtonSprite btnReset;
	
	public final static int LLAYER_BG = 0;
	public final static int LLAYER_MENU = 1;
	public final static int LLAYER_GAME = 2;
	public final static int LAYER_COUNT = 3;
	
	private MoveModifier bgModifier;
	private MoveModifier menuModifier;
	private MoveModifier gameModifier;
	
	public TitleScene(KarmaGame karmaGame) {
		// TODO Auto-generated constructor stub
		this.game = karmaGame;
		this.packer = game.mSpritesheetTexturePackTextureRegionLibrary;
		loadResources();
	}
	
	private void loadResources() {
		this.bgRegion = this.packer.get(TextureData.BACK_ID);
		this.titleRegion = this.packer.get(TextureData.TITLE_ID);
		this.copyrightRegion = this.packer.get(TextureData.COPYRIGHT_ID);
		this.btnStartNormalRegion = this.packer.get(TextureData.BTN_START_NORMAL_ID);
		this.btnResetNormalRegion = this.packer.get(TextureData.BTN_RESET_NORMAL_ID);
		this.btnStartSelectRegion = this.packer.get(TextureData.BTN_START_SELECT_ID);
		this.btnResetSelectRegion = this.packer.get(TextureData.BTN_RESET_SELECT_ID);
		
		this.achieveBackRegion = this.packer.get(TextureData.ACHIEVE_BACK_ID);
		this.bookmarkBackRegion = this.packer.get(TextureData.BOOKMARK_BACK_ID);
		
		this.btnSocialNormalRegion = this.packer.get(TextureData.BTN_SOCIAL_NORMAL_ID);
		this.treeStartNormalRegion = this.packer.get(TextureData.TREE_START_NORMAL_ID);
		
		this.bookmarkBoxRegion = this.packer.get(TextureData.BOOKMARK_BOX_ID);
	}
	
	@Override
	public void drawScene() {
		// TODO Auto-generated method stub
		this.setBackground(new Background(0, 0, 0));
		
		for(int i=0 ; i<LAYER_COUNT ; i++) {
			this.attachChild(new EventEntity(this));
		}
		
		//BG LAYER
		this.bg = new Sprite(0, 0, this.bgRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_BG).attachChild(this.bg);
		
		this.title = new Sprite(198, 67, this.titleRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_MENU).attachChild(this.title);
		
		this.copyright = new Sprite(304, 433, this.copyrightRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_MENU).attachChild(this.copyright);
		
		this.btnStart = new ButtonSprite(111, 324, this.btnStartNormalRegion, this.btnStartSelectRegion, game.getVertexBufferObjectManager(), new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				TitleScene.this.getChildByIndex(LLAYER_BG).registerEntityModifier(bgModifier);
				TitleScene.this.getChildByIndex(LLAYER_MENU).registerEntityModifier(menuModifier);
				TitleScene.this.getChildByIndex(LLAYER_GAME).registerEntityModifier(gameModifier);
			}
		});
		this.registerTouchArea(this.btnStart);
		this.getChildByIndex(LLAYER_MENU).attachChild(this.btnStart);
		
		this.btnReset = new ButtonSprite(425, 324, this.btnResetNormalRegion, this.btnResetSelectRegion, game.getVertexBufferObjectManager(), new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				
				for(int i=0 ; i<game.isPass.length ; i++) {
					game.isPass[i] = false;
				}
				game.mAchieve = 0;
				game.mBookmark = 5;
			}
		});
		this.registerTouchArea(this.btnReset);
		this.getChildByIndex(LLAYER_MENU).attachChild(this.btnReset);
		
		Sprite achieveBack = new Sprite(0, 8, this.achieveBackRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_GAME).attachChild(achieveBack);
		
		Sprite bookmarkBack = new Sprite(497, 18, this.bookmarkBackRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_GAME).attachChild(bookmarkBack);
		
		Sprite btnSocial = new Sprite(681, 363, this.btnSocialNormalRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_GAME).attachChild(btnSocial);
		
		Sprite treeStart = new Sprite(332, 167, this.treeStartNormalRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_GAME).attachChild(treeStart);
		
		Sprite bookmarkBox = new Sprite(696, 18, this.bookmarkBoxRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LLAYER_GAME).attachChild(bookmarkBox);
		
		this.getChildByIndex(LLAYER_GAME).setPosition(KarmaGame.CAMERA_WIDTH, 0);
		
		bgModifier = new MoveModifier(0.5f, 0, -143, 0, 0);
		menuModifier = new MoveModifier(0.5f, 0, -KarmaGame.CAMERA_WIDTH, 0, 0);
		gameModifier = new MoveModifier(0.5f, KarmaGame.CAMERA_WIDTH, 0, 0, 0, new IEntityModifierListener() {

			@Override
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
				// TODO Auto-generated method stub
				game.goGame();
			}
		});
		
		if(Settings.musicEnabled) {
			if(this.game.mBgmMusic.isPlaying()) {
				this.game.mBgmMusic.pause();
			} else {
				this.game.mBgmMusic.play();
			}
		}
	}
}
